package com.t.bricks.bussiness.db.dao.msgmanage.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.msgmanage.MsgManageDao;
import com.t.bricks.bussiness.db.entity.msgmanage.MsgManage;
import com.t.bricks.bussiness.db.mapper.msgmanage.MsgManageMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class MsgManageDaoImpl implements MsgManageDao<MsgManage> {
	
	@Autowired
	private MsgManageMapper msgManageMapper;

	/**
	 * 添加发送失败次数
	 * @param iId 记录编号
	 * @param iType 发送类型(1:邮箱;2:短信;3:微信;4:推送;5:系统消息)
	 * @param iValue 要添加的值(为空则为1)
	 * @return
	 */
	@Override
	public MsgEmity addError(Long iId, Integer iType, Integer iValue) {
		try {
			int i = msgManageMapper.addError(iId, iType, iValue);
			if (i < 1) {
				Log.error("'添加发送失败次数'操作未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "'添加发送失败次数'操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("'添加发送失败次数'操作时发生异常:", e.getMessage());
			return new MsgEmity(false, "'添加发送失败次数'操作失败！", 7002);
		}
		
		return new MsgEmity(true, "'添加发送失败次数'操作成功！", 7999);
	}
	
	/**
	 * 发送成功
	 * @param iId 记录编号
	 * @param iType 发送类型(1:邮箱;2:短信;3:微信;4:推送;5:系统消息)
	 * @return
	 */
	@Override
	public MsgEmity succeed(Long iId, Integer iType) {
		try {
			int i = msgManageMapper.succeed(iId, iType);
			if (i < 1) {
				Log.error("'发送成功'操作未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "'发送成功'操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("'发送成功'操作时发生异常:", e.getMessage());
			return new MsgEmity(false, "'发送成功'操作失败！", 7002);
		}
		
		return new MsgEmity(true, "'发送成功'操作成功！", 7999);
	}
	
	/**
	 * 查询待发送的数据
	 * @param iType 发送类型(0:全部;1:邮箱;2:短信;3:微信;4:推送;5:系统消息)
	 * @param iErrorCount 错误次数限制
	 * @return List<MsgManage>
	 */
	@Override
	public MsgEmity findByType(Integer iType, Integer iErrorCount) {
		List<MsgManage> list = null;
		try {
			list = msgManageMapper.findByType(iType, iErrorCount);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询数据数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

}
